package com.example.testi18n.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

public class RedPacketHandle {
    /**

     * 红包分配算法

     *

     * @param amount

     * @param num

     * @param unit

     * @return

     */

    public static List<BigDecimal> makePackates(BigDecimal amount, Integer num, Integer unit) {

        if (num == 1) {

            return new ArrayList<>(Collections.singletonList(amount));

        }

        List<BigDecimal> prs = new ArrayList<>();

        BigDecimal minAmount = BigDecimal.valueOf(0.00000001);

        BigDecimal randomStart = new BigDecimal(num);

        BigDecimal randomEnd = amount.divide(minAmount);

        for (int i = 0; i < num; i++) {

            prs.add(RandomUtil.random(randomStart, randomEnd));

        }

        BigDecimal totalPr = prs.stream().map(item -> item).reduce(BigDecimal.ZERO, BigDecimal::add);

        totalPr.setScale(unit, RoundingMode.DOWN);

        List<BigDecimal> packages = new ArrayList<>();

        BigDecimal surplus = amount;

        for (BigDecimal pr : prs) {

            if (Objects.equals(pr, prs.get(prs.size() - 1))) {

                packages.add(surplus.setScale(unit, RoundingMode.DOWN));

                continue;

            }

            BigDecimal value = amount.multiply(pr).divide(totalPr, unit, RoundingMode.DOWN).setScale(unit, RoundingMode.DOWN);

            surplus = surplus.subtract(value);

            packages.add(value);

        }

        return packages;

    }

    public static void main(String[] args) {

        /**

         * 红包金额

         */
        BigDecimal amount = new BigDecimal(1000);

        /**

         * 红包个数

         */
        Integer num = 10;

        /**

         * 数值精度(小数位数)

         */
        Integer unit = 8;

        List<BigDecimal> list = makePackates(amount, num, unit);

        for (BigDecimal line : list) {
            System.out.println(line);
        }
        System.err.println("OK");

    }

}